﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_News_NewsCommentMan : System.Web.UI.UserControl
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            this.ShowMess();
            this.SetLabelLanguage(); 
            this.BindDataToGrid(1);
        }
    }

    #region Paging & Message
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        lblKeyword.Text = MultipleLanguage.LBL_KEYWORD[nDefaultLanguage - 1];
        lblStatus.Text = MultipleLanguage.LBL_STATUS[nDefaultLanguage - 1];
        btnSearch.Text = MultipleLanguage.BTN_SEARCH[nDefaultLanguage -1];
        Support.AddStaticData(ddlStatus, Support.GetDefaultLanguage());
    }

    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }

    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }

    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }

    private void ShowMess()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLanguage];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage - 1]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage - 1]);
                break;
            case 3:
                Alert1.ShowMessage(2, MessagesAlert.ITEM_NOTEXIST[nDefaultLanguage - 1]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion

    #region Grid processing

    private void BindDataToGrid(int CurrentPageIndex)
    {
        int nItemView = 0;
        string sSearch = ConvertData.ConvertStringToDB(txtKeyword.Text.Trim());
        int nNewsID = 0;
        int nStatus = ConvertData.ConvertToInt(ddlStatus.SelectedValue);
      
        int nPageSize = Config.PAGESIZE;
        int nPageCount = Config.PAGECOUNT;
    

        DataTable dtbComment = new DataTable();
        NewsComment objComment = new NewsComment();
        dtbComment = objComment.Search(nItemView, sSearch, nNewsID, nStatus, CurrentPageIndex, nPageSize, ref nPageCount);
        int nDefaultLanguage = Support.GetDefaultLanguage();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + " " + nPageCount + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];
        dtgComment.DataSource = dtbComment;
        dtgComment.DataBind();

        Pager1.ItemCount = nPageCount;
        VisiblePaging(nPageCount);
        if (dtbComment.Rows.Count > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;

        txtKeyword.Focus();
        txtKeyword.Text = ConvertData.RemoveHTMLTag(txtKeyword.Text.Trim());
    }

 
    #endregion

    #region Click Events
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindDataToGrid(1);
    }

    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToGrid(1);
    }
    #endregion




    protected void dtgComment_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage()-1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        NewsComment objNewsComment = new NewsComment();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objNewsComment.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMMENT_ADD  + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;

            case "Active":
                int nActive = objNewsComment.UpdateStatus(nID, EnumeType.INACTIVE);
                if (nActive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[nDefaultLanguage]);

                else
                    Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Inactive":
                int nInactive = objNewsComment.UpdateStatus(nID, EnumeType.ACTIVE);
                if (nInactive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;
        }
    }

    protected void dtgComment_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
   
        int nDefaultLanguage = Support.GetDefaultLanguage() ;
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblContent = (Label)e.Item.FindControl("lblContent");
            Label lblNewsTitle = (Label)e.Item.FindControl("lblNewsTitle");
            Label lblDateModified = (Label)e.Item.FindControl("lblDateModified");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");


            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CommentID"));

            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CommentID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLanguage -1]);
            lnkDelete.Enabled = true;
            lnkActive.Enabled = true;


            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));

            string sItemTitle = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Content"));
            if (sItemTitle.Length > 80)
                sItemTitle = ConvertData.TruncateString(sItemTitle, 80) +  Config.DOT;
            lblContent.Text = sItemTitle;

            lblNewsTitle.Text = GetNewsTitleByID( ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID")));
            lblDateModified.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Dateposted")));
            if (nStatus > 0)
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }
           
            lnkActive.Text = MultipleLanguage.STATUS_ACTIVE[nDefaultLanguage - 1];
            lnkInactive.Text = MultipleLanguage.STATUS_INACTIVE[nDefaultLanguage - 1];
            lnkEdit.Text = MultipleLanguage.BTN_READ[nDefaultLanguage - 1];
            lnkDelete.Text = MultipleLanguage.BTN_DELETE[nDefaultLanguage - 1];

            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CommentID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLanguage-1]);

            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CommentID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nDefaultLanguage-1]);
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CommentID"));

          
        }
    }

    private string GetNewsTitleByID(int nID)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        News objNews = new News();
        objNews.LoadById(nID);
        string sReturn = string.Empty;

        switch (nDefaultLanguage)
        { 
            case 1:
                sReturn = objNews.Data.NewsTitle1;
                break;
            case 2:
                sReturn = objNews.Data.NewsTitle2;
                break;
            case 3:
                sReturn = objNews.Data.NewsTitle3;
                break;
        }
        return sReturn;
    }
}
